package com.bruce.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author luotuan
 * @url https://leetcode-cn.com/problems/valid-parentheses/
 * @Description 20. 有效的括号
 * @create 2020-08-03 22:45
 **/
public class ValidBracketDemo {
    public static boolean isValid(String s) {
        if (s == null || "".equals(s)) return true;
        if (s.length() % 2 != 0) return false;
        Map<Character,Character> map = new HashMap<>(3);
        map.put('(',')');
        map.put('{','}');
        map.put('[',']');
        Stack<Character> stack = new Stack<>();
        int size = s.length();
        for (int i=0;i<size;i++) {
            char ch = s.charAt(i);
            if (map.containsKey(ch)) {
                stack.push(ch);
            } else {
                if(stack.isEmpty()) return false;
                char pop = stack.pop();
                if(map.get(pop)!=ch){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(isValid("){"));
    }
}
